package com.guotd.ticketSystem.controller;

import com.guotd.ticketSystem.entity.Flight;
import com.guotd.ticketSystem.entity.Order;
import com.guotd.ticketSystem.mapper.OrderMapper;
import com.guotd.ticketSystem.utils.SessionUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/orderSelect")
public class OrderSelectServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String userId= (String) request.getAttribute("userId");
        if (userId==null) {
            userId = request.getParameter("userId");
        }
            SqlSession session = SessionUtil.getSqlSession();
            OrderMapper orderMapper = session.getMapper(OrderMapper.class);
            List <Order> orders= orderMapper.selectOrders(userId);
            session.commit();
            session.close();
        if (!orders.isEmpty()) {
            request.setAttribute("orders", orders);
            request.getRequestDispatcher("order_list.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("order_list.jsp").forward(request, response);
        }
    }
}